<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css"
          href="jquery/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css">
    <link rel="stylesheet" type="text/css" href="jquery/bs_pagination-master/css/jquery.bs_pagination.min.css">

    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.js"></script>
    <script type="text/javascript"
            src="jquery/bootstrap-datetimepicker-master/locale/bootstrap-datetimepicker.zh-CN.js"></script>
    <script type="text/javascript" src="jquery/bs_pagination-master/js/jquery.bs_pagination.min.js"></script>
    <script type="text/javascript" src="jquery/bs_pagination-master/localization/en.js"></script>

    <script type="text/javascript">
        // 分页查询
        function queryTransactionByConditionForPage(pageNo, pageSize) {
            //收集参数
            var owner = $("#query-owner").val();
            var name = $("#query-name").val();
            var customerName = $("#query-customer-name").val();
            var stage = $("#query-stage").val();
            var type = $("#query-type").val();
            var source = $("#query-source").val();
            var contactsName = $("#query-contacts-name").val();
            //发送请求
            $.ajax({
                url: 'workbench/transaction/queryTransactionByConditionForPage',
                data: {
                    owner: owner,
                    name: name,
                    customerName: customerName,
                    stage: stage,
                    type: type,
                    source: source,
                    contactsName: contactsName,
                    pageNo: pageNo,
                    pageSize: pageSize
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    //显示总条数
                    //$("#totalRowsB").text(data.totalRows);
                    //显示联系人的列表
                    //遍历contactsList，拼接所有行数据
                    var htmlStr = "";
                    $.each(data.transactionList, function (index, obj) {
                        htmlStr += "<tr class=\"active\">";
                        htmlStr += "<td><input type=\"checkbox\" value=\"" + obj.id + "\"/></td>";
                        htmlStr += "<td><a style=\"text-decoration: none; cursor: pointer;\" onclick=\"window.location.href='workbench/transaction/detailById?id=" + obj.id + "'\">" + obj.name + "</a></td>";
                        htmlStr += "<td>" + obj.customerName + "</td>";
                        htmlStr += "<td>" + obj.stage + "</td>";
                        htmlStr += "<td>" + obj.type + "</td>";
                        htmlStr += "<td>" + obj.owner + "</td>";
                        htmlStr += "<td>" + obj.source + "</td>";
                        htmlStr += "<td>" + obj.contactsName + "</td>";
                        htmlStr += "</tr>";
                    });
                    $("#tBody").html(htmlStr);

                    //取消"全选"按钮
                    $("#chckAll").prop("checked", false);

                    //计算总页数
                    var totalPages = 1;
                    if (data.totalRows % pageSize == 0) {
                        totalPages = data.totalRows / pageSize;
                    } else {
                        totalPages = parseInt(data.totalRows / pageSize) + 1;
                    }

                    //对容器调用bs_pagination工具函数，显示翻页信息
                    $("#demo_pag1").bs_pagination({
                        currentPage: pageNo,//当前页号,相当于pageNo

                        rowsPerPage: pageSize,//每页显示条数,相当于pageSize
                        totalRows: data.totalRows,//总条数
                        totalPages: totalPages,  //总页数,必填参数.

                        visiblePageLinks: 5,//最多可以显示的卡片数

                        showGoToPage: true,//是否显示"跳转到"部分,默认true--显示
                        showRowsPerPage: true,//是否显示"每页显示条数"部分。默认true--显示
                        showRowsInfo: true,//是否显示记录的信息，默认true--显示

                        //用户每次切换页号，都自动触发本函数;
                        //每次返回切换页号之后的pageNo和pageSize
                        onChangePage: function (event, pageObj) { // returns page_num and rows_per_page after a link has clicked
                            queryContactsByConditionForPage(pageObj.currentPage, pageObj.rowsPerPage);
                        }
                    });
                }
            });
        }

        $(function () {
            $(".mydate").datetimepicker({
                language: 'zh-CN', //语言
                format: 'yyyy-mm-dd',//日期的格式
                minView: 'month', //可以选择的最小视图
                initialDate: new Date(),//初始化显示的日期
                autoclose: true,//设置选择完日期或者时间之后，日否自动关闭日历
                todayBtn: true,//设置是否显示"今天"按钮,默认是false
                clearBtn: true//设置是否显示"清空"按钮，默认是false
            });

            queryTransactionByConditionForPage(1, 10);

            //给"删除"按钮添加单击事件
            $("#deleteTranBtn").click(function () {
                //获取列表中所有被选中的checkbox
                var chekkedIds = $("#tBody input[type='checkbox']:checked");
                if (chekkedIds.size() == 0) {
                    alert("请选择要删除的交易");
                    return;
                }

                if (window.confirm("确定删除吗？")) {
                    var ids = "";
                    $.each(chekkedIds, function () {
                        ids += "id=" + this.value + "&";
                    });
                    ids = ids.substr(0, ids.length - 1);

                    //发送请求
                    $.ajax({
                        url: 'workbench/transtaction/delete',
                        data: ids,
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            if (data.code == "1") {
                                alert("成功删除~")
                                queryTransactionByConditionForPage(1, $("#demo_pag1").bs_pagination('getOption', 'rowsPerPage'));
                            } else {
                                //提示信息
                                alert(data.message);
                            }
                        }
                    });
                }
            });

            //给"创建"按钮添加单击事件
            $("#createTranBtn").click(function () {
                //重置表单
                $("#createTransactionForm")[0].reset();

                //弹出创建交易的模态窗口
                $("#createTransactionModal").modal("show");
            });

            //给"创建-保存"按钮添加单击事件
            $("#addTranBtn").click(function () {
                //收集参数
                var owner = $("#create-owner").val();
                var money = $.trim($("#create-money").val());
                var name = $.trim($("#create-name").val());
                var expectedDate = $("#create-expectedDate").val();
                var customerName = $.trim($("#create-customerName").val());
                var stage = $("#create-stage").val();
                var type = $("#create-type").val();
                var source = $("#create-source").val();
                var activityId = $("#create-activityId").val();
                var contactsId = $("#create-contactsId").val();
                var description = $.trim($("#create-description").val());
                var contactSummary = $.trim($("#create-contactSummary").val());
                var nextContactTime = $("#create-nextContactTime").val();

                //表单校验
                if (owner == "") {
                    alert("所有者不能为空");
                    return;
                }
                if (name == "") {
                    alert("姓名不能为空");
                    return;
                }
                if (expectedDate == "") {
                    alert("预计成交日期不能为空");
                    return;
                }
                if (customerName == "") {
                    alert("客户名称不能为空");
                    return;
                }
                if (stage == "") {
                    alert("阶段不能为空");
                    return;
                }
                if (type == "") {
                    alert("类型不能为空");
                    return;
                }
                if (source == "") {
                    alert("来源不能为空");
                    return;
                }
                if (activityId == "") {
                    alert("市场活动不能为空");
                    return;
                }
                if (contactsId == "") {
                    alert("联系人不能为空");
                    return;
                }
                var regExp = /^(([1-9]\d*)|0)$/;
                if (!regExp.test(money)) {
                    alert("金额只能为非负整数");
                    return;
                }
                //发送请求
                $.ajax({
                    url: 'workbench/transaction/add',
                    data: {
                        owner: owner,
                        money: money,
                        name: name,
                        expectedDate: expectedDate,
                        customerName: customerName,
                        stage: stage,
                        type: type,
                        source: source,
                        activityId: activityId,
                        contactsId: contactsId,
                        description: description,
                        contactSummary: contactSummary,
                        nextContactTime: nextContactTime
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.code == "1") {
                            $("#createTransactionModal").modal("hide");
                            queryTransactionByConditionForPage(1, $("#demo_pag1").bs_pagination('getOption', 'rowsPerPage'));
                        } else {
                            //提示信息
                            alert(data.message);
                            $("#createTransactionModal").modal("show");//可以不写。
                        }
                    }
                });
            });

            //给"修改"按钮添加单击事件
            $("#toUpdateTranBtn").click(function () {

                var chkedIds = $("#tBody input[type='checkbox']:checked");
                if (chkedIds.size() == 0) {
                    alert("请选择要修改的交易");
                    return;
                }
                if (chkedIds.size() > 1) {
                    alert("每次只能修改一个交易");
                    return;
                }

                var id = chkedIds[0].value;
                window.location.href = "workbench/transaction/toUpdate?id=" + id;
            });


            //给"创建-市场活动源"搜索按钮添加单击事件
            $("#searchActivityBtn").click(function () {
                $("#searchActivityTxt").val("");
                //清空搜索列表
                $("#activityTBody").html("");

                //弹出搜索市场活动的模态窗口
                $("#searchActivityModal").modal("show");
            });

            //给"创建-市场活动源"搜索框添加键盘弹起事件
            $("#searchActivityTxt").keyup(function () {
                //收集参数
                var activityName = this.value;
                //发送请求
                $.ajax({
                    url: 'workbench/activity/queryActivityLikeName',
                    data: {
                        activityName: activityName
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        //遍历data,显示所有搜索的市场活动
                        var htmlStr = "";
                        $.each(data, function (index, obj) {
                            htmlStr += "<tr>";
                            htmlStr += "<td><input type=\"radio\" value=\"" + obj.id + "\" activityName=\"" + obj.name + "\" name=\"activity\"/></td>";
                            htmlStr += "<td>" + obj.name + "</td>";
                            htmlStr += "<td>" + obj.startDate + "</td>";
                            htmlStr += "<td>" + obj.endDate + "</td>";
                            htmlStr += "<td>" + obj.owner + "</td>";
                            htmlStr += "</tr>";
                        });
                        $("#activityTBody").html(htmlStr);
                    }
                });
            });

            //给"创建-市场活动源"的单选按钮添加单击事件
            $("#activityTBody").on("click", "input[type='radio']", function () {
                //获取市场活动的id和name
                var id = this.value;
                var activityName = $(this).attr("activityName");
                //把市场活动的id写到隐藏域，把name写到输入框中
                $("#create-activityId").val(id);
                $("#create-activityName").val(activityName);
                //关闭搜索市场活动的模态窗口
                $("#searchActivityModal").modal("hide");
            });

            //给"创建-联系人名称"搜索按钮添加单击事件
            $("#searchContactsBtn").click(function () {
                $("#searchContactsTxt").val("");
                //清空搜索列表
                $("#contactsTBody").html("");

                //弹出搜索联系人的模态窗口
                $("#searchContactsModal").modal("show");
            });

            //给"创建-联系人名称"搜索框添加键盘弹起事件
            $("#searchContactsTxt").keyup(function () {
                //收集参数
                var contactsName = this.value;
                //发送请求
                $.ajax({
                    url: 'workbench/contacts/queryContactsLikeName',
                    data: {
                        contactsName: contactsName
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        //遍历data,显示所有搜索的市场活动
                        var htmlStr = "";
                        $.each(data, function (index, obj) {
                            htmlStr += "<tr>";
                            htmlStr += "<td><input type=\"radio\" value=\"" + obj.id + "\" contactsName=\"" + obj.fullname + "\" name=\"contacts\"/></td>";
                            htmlStr += "<td>" + obj.fullname + "</td>";
                            htmlStr += "<td>" + obj.email + "</td>";
                            htmlStr += "<td>" + obj.mphone + "</td>";
                            htmlStr += "</tr>";
                        });
                        $("#contactsTBody").html(htmlStr);
                    }
                });
            });

            //给"创建-联系人名称"的单选按钮添加单击事件
            $("#contactsTBody").on("click", "input[type='radio']", function () {
                //获取市场活动的id和name
                var id = this.value;
                var contactsName = $(this).attr("contactsName");
                //把市场活动的id写到隐藏域，把name写到输入框中
                $("#create-contactsId").val(id);
                $("#create-contactsName").val(contactsName);
                //关闭搜索市场活动的模态窗口
                $("#searchContactsModal").modal("hide");
            });
        });
    </script>
</head>
<body>

<!-- 创建交易的模态窗口 -->
<div class="modal fade" id="createTransactionModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabelx">创建交易</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="createTransactionForm" role="form">
                    <div class="form-group">
                        <label for="create-owner" class="col-sm-2 control-label">所有者<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-owner">
                                <c:forEach items="${userList}" var="u">
                                    <option value="${u.id}">${u.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <label for="create-money" class="col-sm-2 control-label">金额</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-money">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-name" class="col-sm-2 control-label">名称<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-name">
                        </div>
                        <label for="create-expectedDate" class="col-sm-2 control-label">预计成交日期<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control mydate" name="mydate" readonly
                                   id="create-expectedDate">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-customerName" class="col-sm-2 control-label">客户名称<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-customerName"
                                   placeholder="支持自动补全，输入客户不存在则新建">
                        </div>
                        <label for="create-stage" class="col-sm-2 control-label">阶段<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-stage">
                                <option></option>
                                <c:forEach items="${stageList}" var="s">
                                    <option value="${s.id}">${s.value}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-type" class="col-sm-2 control-label">类型</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-type">
                                <option></option>
                                <c:forEach items="${transactionTypeList}" var="tt">
                                    <option value="${tt.id}">${tt.value}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <label for="create-possibility" class="col-sm-2 control-label">可能性</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-possibility" readonly>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-source" class="col-sm-2 control-label">来源</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="create-source">
                                <option></option>
                                <c:forEach items="${sourceList}" var="so">
                                    <option value="${so.id}">${so.value}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <label for="create-activityName" class="col-sm-2 control-label">市场活动源&nbsp;&nbsp;<a
                                href="javascript:void(0);"
                                id="searchActivityBtn"
                                data-toggle="modal">
                            <span class="glyphicon glyphicon-search"></span></a></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="hidden" id="create-activityId">
                            <input type="text" class="form-control" id="create-activityName" readonly>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-contactsName" class="col-sm-2 control-label">联系人名称&nbsp;&nbsp;
                            <a
                                    href="javascript:void(0);"
                                    data-toggle="modal"
                                    id="searchContactsBtn">
                                <span class="glyphicon glyphicon-search"></span>
                            </a>
                        </label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="hidden" id="create-contactsId">
                            <input type="text" class="form-control" id="create-contactsName" readonly>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-description" class="col-sm-2 control-label">描述</label>
                        <div class="col-sm-10" style="width: 70%;">
                            <textarea class="form-control" rows="3" id="create-description"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-contactSummary" class="col-sm-2 control-label">联系纪要</label>
                        <div class="col-sm-10" style="width: 70%;">
                            <textarea class="form-control" rows="3" id="create-contactSummary"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-nextContactTime" class="col-sm-2 control-label">下次联系时间</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control mydate" name="mydate" readonly
                                   id="create-nextContactTime">
                        </div>
                    </div>
                </form>
            </div>
            <div style="position: relative; top: -40px; left: 70%;">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="addTranBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 查找市场活动模态窗口 -->
<div class="modal fade" id="searchActivityModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">查找市场活动</h4>
            </div>
            <div class="modal-body">
                <div class="btn-group" style="position: relative; top: 18%; left: 8px;">
                    <form class="form-inline" role="form">
                        <div class="form-group has-feedback">
                            <input id="searchActivityTxt" type="text" class="form-control" style="width: 300px;"
                                   placeholder="请输入市场活动名称，支持模糊查询">
                            <span class="glyphicon glyphicon-search form-control-feedback"></span>
                        </div>
                    </form>
                </div>
                <table id="activityTable3" class="table table-hover"
                       style="width: 900px; position: relative;top: 10px;">
                    <thead>
                    <tr style="color: #B3B3B3;">
                        <td></td>
                        <td>名称</td>
                        <td>开始日期</td>
                        <td>结束日期</td>
                        <td>所有者</td>
                    </tr>
                    </thead>
                    <tbody id="activityTBody">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 查找联系人模态窗口 -->
<div class="modal fade" id="searchContactsModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">查找联系人</h4>
            </div>
            <div class="modal-body">
                <div class="btn-group" style="position: relative; top: 18%; left: 8px;">
                    <form class="form-inline" role="form">
                        <div class="form-group has-feedback">
                            <input type="text" class="form-control" id="searchContactsTxt" style="width: 300px;"
                                   placeholder="请输入联系人名称，支持模糊查询">
                            <span class="glyphicon glyphicon-search form-control-feedback"></span>
                        </div>
                    </form>
                </div>
                <table id="activityTable" class="table table-hover" style="width: 900px; position: relative;top: 10px;">
                    <thead>
                    <tr style="color: #B3B3B3;">
                        <td></td>
                        <td>名称</td>
                        <td>邮箱</td>
                        <td>手机</td>
                    </tr>
                    </thead>
                    <tbody id="contactsTBody">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div>
    <div style="position: relative; left: 10px; top: -10px;">
        <div class="page-header">
            <h3>交易列表</h3>
        </div>
    </div>
</div>

<%--详细信息--%>
<div style="position: relative; top: -20px; left: 0px; width: 100%; height: 100%;">
    <div style="width: 100%; position: absolute;top: 5px; left: 10px;">

        <div class="btn-toolbar" role="toolbar" style="height: 80px;">
            <form class="form-inline" role="form" style="position: relative;top: 8%; left: 5px;">

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">所有者</div>
                        <input class="form-control" type="text" id="query-owner">
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">名称</div>
                        <input class="form-control" type="text" id="query-name">
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">客户名称</div>
                        <input class="form-control" type="text" id="query-customer-name">
                    </div>
                </div>

                <br>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">阶段</div>
                        <select class="form-control" id="query-stage">
                            <option></option>
                            <c:forEach items="${stageList}" var="s">
                                <option value="${s.id}">${s.value}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">类型</div>
                        <select class="form-control" id="query-type">
                            <option></option>
                            <c:forEach items="${transactionTypeList}" var="tt">
                                <option value="${tt.id}">${tt.value}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">来源</div>
                        <select class="form-control" id="query-source">
                            <option></option>
                            <c:forEach items="${sourceList}" var="so">
                                <option value="${so.id}">${so.value}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">联系人名称</div>
                        <input class="form-control" type="text" id="query-contacts-name">
                    </div>
                </div>

                <button type="button" class="btn btn-default">查询</button>

            </form>
        </div>
        <div class="btn-toolbar" role="toolbar"
             style="background-color: #F7F7F7; height: 50px; position: relative;top: 10px;">
            <div class="btn-group" style="position: relative; top: 18%;">
                <button type="button" class="btn btn-primary" id="createTranBtn"><span
                        class="glyphicon glyphicon-plus"></span> 创建
                </button>
                <button type="button" class="btn btn-default" id="toUpdateTranBtn"><span
                        class="glyphicon glyphicon-pencil"></span> 修改
                </button>
                <button type="button" class="btn btn-danger" id="deleteTranBtn"><span
                        class="glyphicon glyphicon-minus"></span> 删除
                </button>
            </div>


        </div>
        <div style="position: relative;top: 10px;">
            <table class="table table-hover">
                <thead>
                <tr style="color: #B3B3B3;">
                    <td><input type="checkbox"/></td>
                    <td>名称</td>
                    <td>客户名称</td>
                    <td>阶段</td>
                    <td>类型</td>
                    <td>所有者</td>
                    <td>来源</td>
                    <td>联系人名称</td>
                </tr>
                </thead>
                <tbody id="tBody">
                </tbody>
            </table>
            <div id="demo_pag1"></div>
        </div>
    </div>

</div>
</body>
</html>